package org.rslai.autotest.core.page.impl;

import java.util.Set;

import org.rslai.autotest.core.page.AbstractHelper;
import org.rslai.autotest.core.page.Cookie;

public class CookieImpl extends AbstractHelper implements Cookie {

	@Override
	public Set<org.openqa.selenium.Cookie> getCookies() {
		return super.driver().manage().getCookies();
	}

	@Override
	public String getCookiesString() {
		StringBuffer sb = new StringBuffer();
		Set<org.openqa.selenium.Cookie> cookiesSet = this.getCookies();
		for (org.openqa.selenium.Cookie c : cookiesSet) {
			sb.append("[");
			sb.append(c.getName());
			sb.append("=");
			sb.append(c.getValue());
			sb.append("]\r\n");
		}
		return sb.toString();
	}

	@Override
	public boolean isCookie(String name) {
		if (super.driver().manage().getCookieNamed(name) == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public org.openqa.selenium.Cookie getCookie(String name) {
		return super.driver().manage().getCookieNamed(name);
	}

	@Override
	public String getCookieValue(String name) {
		return this.getCookie(name).getValue();
	}

	@Override
	public void addCookie(org.openqa.selenium.Cookie cookie) {
		super.driver().manage().addCookie(cookie);
	}

	@Override
	public void deleteCookie(org.openqa.selenium.Cookie cookie) {
		super.driver().manage().deleteCookie(cookie);
	}

	@Override
	public void deleteCookie(String name) {
		org.openqa.selenium.Cookie cookie = super.driver().manage().getCookieNamed(name);
		if (cookie != null) {
			this.deleteCookie(cookie);
		}
	}

	@Override
	public void deleteAllCookies() {
		super.driver().manage().deleteAllCookies();
	}

}
